/*
 * Created on Mar 1, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.tree.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.event.*;
import java.io.*;

import ModuleBuilder.model.*;
import ModuleBuilder.UI.*;
import ModuleBuilder.UI.tree.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Storer {
	private static JFileChooser fc = new JFileChooser();
	static String[] scenarios = new String[] {".scn"};
	
	public static ScenarioModel getScenarioModel( BuilderManager manager, JFrame parent ) {
		ScenarioModel scenarioMod = new ScenarioModel();
		
		PictureBuilder topFigBuilder = manager.getFigureTopBuilder();
		PictureBuilder sideFigBuilder = manager.getFigureSideBuilder();
		PictureBuilder terrainTileBuilder = manager.getMapTileBuilder();
		GameParameter gameParameter = manager.getGameParameter();
		MapBuilder mapBuilder = manager.getMapBuilder();
		GameTreeEditor treeEditor = manager.getGameTreeEditor();

		scenarioMod.setTopFigurePix(topFigBuilder.store());
		scenarioMod.setSideFigurePix(sideFigBuilder.store());
		scenarioMod.setMapTilePix(terrainTileBuilder.store());
		scenarioMod.setGameParamModel(gameParameter.store());
		scenarioMod.setMapModel(mapBuilder.store());
		scenarioMod.setTreeRoot(treeEditor.store());
		
		//Show it.
		fc.resetChoosableFileFilters();
		fc.addChoosableFileFilter(new SimpleFileFilter(scenarios, "Scenario Builder Scenario files"));
		int returnVal = fc.showDialog(parent, "Select Scenario File Name for Save");
		File file=null;
		
		//Process the results.
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
			String fn = file.getPath();

			// make sure the file ends in the right suffix
			String lowerCaseFn = fn.toLowerCase();
			if (!lowerCaseFn.endsWith(scenarios[0])){
				fn += scenarios[0];
				file = new File(fn);
			}
			
			// now serialize the result
			FileOutputStream fo=null;
			BufferedOutputStream bo=null;
			ObjectOutputStream oo = null;
			try {
				fo = new FileOutputStream(file);
				bo = new BufferedOutputStream(fo);
				oo = new ObjectOutputStream(bo);
				oo.writeObject(scenarioMod);
			
				oo.flush();
				oo.close();
			
			} catch (Exception ex) {
				ex.printStackTrace();
			}

		} else {
			Debug.out("Select Scenario File cancelled by user.");
		}
		return ( scenarioMod );
	}
	/**
	 * @return
	 */
	public static JFileChooser getFc() {
		return fc;
	}

}
